package com.copm.demonstrationsystem.controller.housetypefeatures;

import com.copm.demonstrationsystem.commons.response.ResponseStatus;
import com.copm.demonstrationsystem.commons.util.ControllerUtils;
import com.copm.demonstrationsystem.commons.util.ImageUtils;
import com.copm.demonstrationsystem.dto.housetypefeatures.HouseTypeImageReceiveDto;
import com.copm.demonstrationsystem.service.housetypefeatures.HouseTypeFeaturesService;
import io.swagger.annotations.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.util.UUID;

/**
 * @author chuyunfei
 * @date: 2018/4/17
 * @description: 1、实现房间类型的增删改查； 2、实现户型的增删改查 3、实现户型图片的增加和删除
 */
@RestController
@RequestMapping("/house")
@CrossOrigin//解决跨域请求
public class HouseTypeFeaturesController {

    private Logger logger = LoggerFactory.getLogger(HouseTypeFeaturesController.class);

    private HouseTypeFeaturesService service;

    @ApiOperation("房间类型添加接口")
    @ApiImplicitParam(name = "name", value = "新的房间类型的名称", dataType = "String", paramType = "query", required = true)
    @RequestMapping(value = "/roomtype", method = RequestMethod.POST)
    ResponseStatus addRoomType(@RequestParam(value = "name") String name){
        logger.info("添加房间类型：" + name);
        return ControllerUtils.fillResponse(new ResponseStatus(),service.addRoomType(name));
    }

    @ApiOperation("房间类型删除接口")
    @ApiImplicitParam(name = "id", value = "房间类型的ID", dataType = "Long", paramType = "path", required = true)
    @RequestMapping(value = "/roomtype/{id}", method = RequestMethod.DELETE)
    ResponseStatus deleteRoomType(@PathVariable("id") Long id){
        logger.info("删除房间类型：" + id);
        return ControllerUtils.fillResponse(new ResponseStatus(),service.deleteRoomType(id));
    }

    @ApiOperation("获取全部房间类型接口")
    @RequestMapping(value = "/roomtype", method = RequestMethod.GET)
    ResponseStatus getRoomTypes(){
        logger.info("获取全部房间类型");
        return ControllerUtils.fillResponse(new ResponseStatus(),service.getRoomTypes());
    }

    @ApiOperation("户型添加接口")
    @ApiImplicitParam(name = "name", value = "新的户型的名称", dataType = "String", paramType = "query", required = false)
    @RequestMapping(value = "/housetype", method = RequestMethod.POST)
    ResponseStatus addHouseType(@RequestParam(value = "name", required = false) String name){

        logger.info("添加户型：" + name);
        //户型的添加不会传递户型名称，所以随机生成字符串来进行存储
        if(name == null){
            name = UUID.randomUUID().toString();
        }
        return ControllerUtils.fillResponse(new ResponseStatus(),service.addHouseType(name));
    }

    @ApiOperation("户型删除接口")
    @ApiImplicitParam(name = "id", value = "户型的ID", dataType = "Long", paramType = "path", required = true)
    @RequestMapping(value = "/housetype/{id}", method = RequestMethod.DELETE)
    ResponseStatus deleteHouseType(@PathVariable("id") Long id){
        logger.info("删除户型：" + id);
        return ControllerUtils.fillResponse(new ResponseStatus(),service.deleteHouseType(id));
    }

    @ApiOperation("获取全部户型接口，包括里面的所有的图片")
    @RequestMapping(value = "/housetype", method = RequestMethod.GET)
    ResponseStatus getHouseTypes(){
        logger.info("获取全部户型");
        return ControllerUtils.fillResponse(new ResponseStatus(),service.getHouseTypes());
    }

    @ApiOperation("获取指定户型接口")
    @ApiImplicitParam(name = "id", value = "户型的ID", dataType = "Long", paramType = "path", required = true)
    @RequestMapping(value = "/housetype/{id}", method = RequestMethod.GET)
    ResponseStatus getHouseType(@PathVariable("id") Long id){
        logger.info("获取指定户型：" + id);
        return ControllerUtils.fillResponse(new ResponseStatus(),service.getHouseType(id));
    }

    @ApiOperation("户型图片添加接口")
    @ApiImplicitParams({@ApiImplicitParam(name = "file", value = "上传图片的form-data数据对象", dataType = "file", paramType = "form", required = true),@ApiImplicitParam(name = "houseTypeId", value = "上传图片的户型ID", dataType = "Long", paramType = "query", required = true),@ApiImplicitParam(name = "roomTypeId", value = "上传图片的房间类型ID", dataType = "Long", paramType = "query", required = false),@ApiImplicitParam(name = "kind", value = "上传图片的图片分类", dataType = "Long", paramType = "query", required = true)})
    @RequestMapping(value = "/image", method = RequestMethod.POST)
    ResponseStatus addHouseTypeImage(
            @RequestParam("kind")Long kind
            ,@RequestParam("houseTypeId")Long houseTypeId
            ,@RequestParam(value = "roomTypeId",required = false,defaultValue = "-1")Long roomTypeId,
            @RequestParam("file")MultipartFile multipartFile) throws IOException{

        HouseTypeImageReceiveDto imageReceiveDto = new HouseTypeImageReceiveDto(houseTypeId,roomTypeId==null?0L:roomTypeId,kind);

        logger.info("添加户型照片：" + imageReceiveDto + ": " + multipartFile.getOriginalFilename());
        //检查图片的格式
        Boolean suffixEnable = ImageUtils.checkSuffix(multipartFile.getOriginalFilename());
        if(!suffixEnable){
            return new ResponseStatus(0,"图片格式不正确",null);
        }

        return ControllerUtils.fillResponse(new ResponseStatus(),service.addHouseTypeImage(imageReceiveDto,multipartFile));

        //        byte[] fileBytes = multipartFile.getBytes();
        //
        //        File file = new File("D:/" + multipartFile.getOriginalFilename());
        //
        //        BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(file));
        //
        //        outputStream.write(fileBytes);
        //
        //        outputStream.flush();
        //
        //        outputStream.close();

        //        System.out.println(file.getOriginalFilename() + "dcfghjk");
        //
        //        HouseTypeImageReceiveDto imageReceiveDto = new HouseTypeImageReceiveDto(houseTypeId,roomTypeId,kind);
        //
        //        System.out.println(imageReceiveDto);
        //
        //        Boolean suffixEnable = ImageUtils.checkSuffix(file.getOriginalFilename());
        //
        //        if(!suffixEnable){
        //            return new ResponseStatus(0,"图片格式不正确",null);
        //        }

//        return new ResponseStatus();//ControllerUtils.fillResponse(new ResponseStatus(),service.addHouseTypeImage(imageReceiveDto, file));
    }

    //3.2、
    @ApiOperation("户型图片删除接口")
    @ApiImplicitParam(name = "id", value = "户型图片的ID", dataType = "Long", paramType = "path", required = true)
    @RequestMapping(value = "/image/{id}", method = RequestMethod.DELETE)
    ResponseStatus deleteHouseTypeImage(@PathVariable("id") Long id){
        logger.info("删除指定户型图片：" + id);
        return ControllerUtils.fillResponse(new ResponseStatus(),service.deleteHouseTypeImage(id));
    }

    @Autowired
    public void setService(HouseTypeFeaturesService service){
        this.service = service;
    }
}
